package cn.gong.movie.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import cn.gong.movie.adapter.AdapMovie;
import cn.gong.movie.bean.Movie;
import cn.gong.movie.data.DataMovie;
import framework.base.adapter.MoreView;
import framework.base.ui.BaseAct;
import framework.base.ui.ClickMethod;
import framework.base.ui.InjectView;

public class ActMovie extends BaseAct {

	private static final int REQUEST_CODE_ADD_MOVIE = 0;
	private static final int REQUEST_CODE_EDIT_MOVIE = 1;
	@InjectView(R.id.movie_bt_want)
	private Button wantBt;
	@InjectView(R.id.movie_bt_seen)
	private Button seenBt;
	@InjectView(R.id.movie_lv)
	private ListView movieLv;

	private boolean isWant = true;
	private long lastId = -1;
	private AdapMovie adapter;
	private boolean hasMore = true;
	private int status = 0;

	private Movie oprateMovie;
	private AlertDialog alertDialog;
	protected AlertDialog ad;

	@Override
	protected void onCreateAct(Bundle savedInstanceState) {
		super.onCreateAct(savedInstanceState);
		setContentView(R.layout.movie);
		initTitle();
		initLv();
		DataMovie.getMovies(lastId, status, this);
	}

	private void initLv() {
		adapter = new AdapMovie(new MoreView() {

			@Override
			public View moreView() {
				View inflate = inflate(R.layout.more_item);
				TextView moreTv = findTextViewById(R.id.more_item_tv, inflate);
				if (hasMore) {
					setText(moreTv, "更多");
				} else {
					setText(moreTv, "没有更多影片了");
				}
				return inflate;
			}
		});
		movieLv.setAdapter(adapter);
		movieLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				oprateMovie = (Movie) adapter.getItem(position);
				if (oprateMovie == null) {
					// more
					if (hasMore) {
						DataMovie.getMoreMovies(lastId, status, getAct());
					}
				} else {
					// movie
					ad = new AlertDialog.Builder(getAct()).create();
					View inflate = inflate(R.layout.option);
					ImageButton closeIb = findImageButtonById(R.id.option_ib_close, inflate);
					closeIb.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (ad != null) {
								ad.cancel();
							}
						}
					});
					TextView moveTv = findTextViewById(R.id.option_tv_move, inflate);
					setText(moveTv, status == 0 ? "已看" : "想看");
					setClick(moveTv);
					setClick(R.id.option_tv_edit, inflate);
					setClick(R.id.option_tv_delete, inflate);
					ad.setView(inflate);
					ad.show();
				}

			}
		});
	}

	private void initTitle() {
		TextView titleTv = findTextViewById(R.id.title_bar_tv_title);
		setText(titleTv, "电影");
		viewGone(R.id.title_bar_ib_left);
	}

	@ClickMethod(R.id.option_tv_move)
	protected void clickMove(View view) {
		// 更改status
		dialogCancel(ad);
		alertDialog = getAlertDialog();
		DataMovie.changeStatus(oprateMovie.getId(), status == 0 ? 1 : 0, this);
	}

	protected void dbTsChangeMovieStatus(Object data) {
		adapter.deleteMovie(oprateMovie);
		dialogCancel(alertDialog);
	}

	@ClickMethod(R.id.option_tv_edit)
	protected void clickEdit(View view) {
		dialogCancel(ad);
		Intent intent = new Intent(getApplicationContext(), ActEditMovie.class);
		intent.putExtra("movie", oprateMovie);
		openActForResult(intent, REQUEST_CODE_EDIT_MOVIE);

		animTranslateBottomIn();
	}

	@ClickMethod(R.id.option_tv_delete)
	protected void clickDelete(View view) {
		dialogCancel(ad);
		alertDialog = getAlertDialog();
		DataMovie.deleteMovie(oprateMovie.getId(), this);
	}

	protected void dbTsDeleteMovie(Object data) {
		adapter.deleteMovie(oprateMovie);
		dialogCancel(alertDialog);
	}

	@ClickMethod(R.id.title_bar_ib_right)
	protected void clickClose(View view) {
		closeAct();
		animScaleClose();
	}

	@ClickMethod(R.id.movie_bt_want)
	protected void clickWant(View view) {
		if (!isWant) {
			isWant = true;
			wantBt.setBackgroundResource(R.drawable.bg_tab_select);
			seenBt.setBackgroundResource(R.drawable.bg_tab_unselect);
			lastId = -1;
			status = 0;
			hasMore = true;
			DataMovie.getMovies(lastId, status, this);
		}
	}

	@ClickMethod(R.id.movie_bt_seen)
	protected void clickSeen(View view) {
		if (isWant) {
			isWant = false;
			wantBt.setBackgroundResource(R.drawable.bg_tab_unselect);
			seenBt.setBackgroundResource(R.drawable.bg_tab_select);
			lastId = -1;
			status = 1;
			hasMore = true;
			DataMovie.getMovies(lastId, status, this);
		}
	}

	@ClickMethod(R.id.movie_ll_add)
	protected void clickAdd(View view) {
		openActForResult(ActEditMovie.class, REQUEST_CODE_ADD_MOVIE);
		animTranslateBottomIn();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case REQUEST_CODE_ADD_MOVIE:
				addMovieOk(data);
				break;
			case REQUEST_CODE_EDIT_MOVIE:
				editMoiveOk(data);
				break;
			}
		}
	}

	private void editMoiveOk(Intent data) {
		Movie movie = (Movie) data.getSerializableExtra(Strings.INTENT_EXTRA_MOVIE);
		oprateMovie.copy(movie);
		adapter.notifyDataSetChanged();
	}

	private void addMovieOk(Intent data) {
		Movie movie = (Movie) data.getSerializableExtra(Strings.INTENT_EXTRA_MOVIE);
		if (status == 0 && movie != null && movie.getId() >= 0) {
			hasMore = true;
			if (lastId == -1) {
				lastId = movie.getId();
			}
			adapter.addListFirst(movie);
		}
	}

	protected void dbTsGetMovies(Object data) {
		ArrayList<Movie> movies = (ArrayList<Movie>) data;
		if (movies != null && movies.size() > 0) {
			lastId = movies.get(movies.size() - 1).getId();
		} else {
			hasMore = false;
		}
		adapter.setList(movies);

	}

	protected void dbTsGetMoreMovies(Object data) {
		try {
			ArrayList<Movie> movies = (ArrayList<Movie>) data;
			if (movies != null && movies.size() > 0) {
				lastId = movies.get(movies.size() - 1).getId();
			} else {
				hasMore = false;
			}
			adapter.addList(movies);
		} catch (Exception e) {
			exception(e);
		}
	}
}
